package com.business.action.admin.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.business.bean.system.UserBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.web.util.SessionUtils;

@Controller
@RequestMapping("/online")
public class OnlineAction {

	/**
	 * 获取所有的在线用户数据
	 */
	@ResponseBody
	@RequestMapping("/allUsers.do")
	public PageBean getAllOnlineUsers() {
		PageBean page = new PageBean();
		Map<String, SessionUtils> map = SessionUtils.getSessionCache();
		List<Map<String, Object>> list = new ArrayList<>();
		for (String key : map.keySet()) {
			SessionUtils bean = map.get(key);
			Map<String, Object> m = new HashMap<>();
			m.put("lastLoginTime", bean.getToday());
			UserBean user = bean.getData();
			m.put("userName", user.getLoginName());
			m.put("key", key);
			list.add(m);
		}
		page.setData(list);
		return page;
	}

	/**
	 * 删除某个在线用户数据
	 */
	@ResponseBody
	@RequestMapping("/removeUser.do")
	public PageBean removeUser(String key) {
		PageBean page = new PageBean();
		if (SessionUtils.getSessionCache().remove(key) == null) {
			page.setSuccess(false);
		}
		return page;
	}
}